package ece496.gps;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class MapDetectionActivity extends Activity {

	FeatureListObject featureList = new FeatureListObject();
	
	TextView tV;
   /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tV = (TextView) findViewById(R.id.textView1);

        try {
    		parseMapFeatures();
    	} catch (IOException e) {
    		Toast.makeText(getApplicationContext(), "Problems: " + e.getMessage(), 1).show();
            Log.e("fileIO", "IOException Error:" + e.getMessage());
    	}
        Log.i("featureDetect", "Finished parsing map features");
        
        // Self test by starting activity, parsing and detecting at coordinates here
        for (int i = 0; i < 5; i++) {
        	int x = testRandom.nextInt(150);
        	int y = testRandom.nextInt(150);
	    	tV.append("\nCurrent coordinates are x:"+x+" and y:"+y+"\n");

		    /*FeatureObject closestFeature = detectMapFeature(x,y);
		    if (closestFeature != null) {
		    	Toast.makeText(MapDetectionActivity.this, "Detected closest feature "+closestFeature.featureType, Toast.LENGTH_LONG).show();
		    	if (closestFeature.featureType.equals("WALL"))
		    		tV.append("Detected closest feature "+closestFeature.featureType+" with coordinates xStart:"+closestFeature.xStart+", xEnd:"+closestFeature.xEnd+" and yStart:"+closestFeature.yStart+", yEnd:"+closestFeature.yEnd+"\n");
		    	else
		    		tV.append("Detected closest feature "+closestFeature.featureType+" with coordinates x:"+closestFeature.xStart+" y:"+closestFeature.yStart+"\n");
		    }
		    else {
		    	Toast.makeText(MapDetectionActivity.this, "Error! Cannot find feature", Toast.LENGTH_LONG).show();
		    	tV.setText("Error! Cannot find closest feature!");
		    }
		    */
		    int x2 = testRandom.nextInt(150);
		    int y2 = testRandom.nextInt(150);
		    tV.append("Other coordinates are x2:"+x2+" and y2:"+y2+"\n");
		    FeatureObject intersect = featureList.findClosestWallIntersect(x, x2, y, y2);
		    if (intersect != null) {
		    	tV.append("Detected wall intersection at point x:"+intersect.xStart+" and y:"+intersect.yStart+"\n");		    	
		    }
		    else {
		    	tV.append("No possible wall intersections\n");
		    }
        }
    }

    public FeatureObject detectMapFeature (int x, int y) {
		FeatureObject foundFeature = featureList.findClosestFeature(x, y);
		
		return foundFeature;
    }
    	
    public void parseMapFeatures () throws IOException {
    	// Open file stream and create buffered reader
    	String featureStr = null;
    	InputStream MapIS = this.getResources().openRawResource(R.drawable.map_features);
    	BufferedReader reader = new BufferedReader(new InputStreamReader(MapIS));
    	
    	Log.i("parseMapFeatures", "Finished opening file map_features.txt");
    	// Read line by line
    	if (MapIS != null) {
    		while ((featureStr = reader.readLine()) != null) {
    			// Create new feature class
    	    	FeatureObject tempFeature = new FeatureObject();
    	    	String [] tempSplit = null;
    			
    	    	Log.i("parseMapFeatures", "Read string: "+featureStr);
    	    	// Split line into feature type and coordinates, use tokenizer to split into parts
    	    	// Expected data line example:
    	    	// WALL::(x1,y1)(x2,y2)
    	    	tempSplit = featureStr.split("::");
    			StringTokenizer ST = new StringTokenizer(tempSplit[1], ",-()");
    			
    			Log.i("parseMapFeatures", "Split string into: "+tempSplit[0]+" and: "+tempSplit[1]);
    			if (tempSplit[0].contains("WALL")) {
    				while (ST.hasMoreTokens()) {
    					tempFeature.xStart = Integer.parseInt(ST.nextToken());
    					tempFeature.yStart = Integer.parseInt(ST.nextToken());
    					tempFeature.xEnd = Integer.parseInt(ST.nextToken());
    					tempFeature.yEnd = Integer.parseInt(ST.nextToken());
    				}
    				tempFeature.featureType = "WALL";
    				featureList.addFeature("WALL", tempFeature);
    				Log.i("parseMapFeatures", "Added feature WALL with coordinates x:"+tempFeature.xStart+" y:"+tempFeature.yStart);
    			}
    			else if (tempSplit[0].contains("DOOR")) {
    				while (ST.hasMoreTokens()) {
    					tempFeature.xStart = Integer.parseInt(ST.nextToken());
    					tempFeature.xEnd = tempFeature.xStart;
    					tempFeature.yStart = Integer.parseInt(ST.nextToken());
    					tempFeature.yEnd = tempFeature.yStart;
    				}
    				tempFeature.featureType = "DOOR";
    				featureList.addFeature("DOOR", tempFeature);
    				Log.i("parseMapFeatures", "Added feature DOOR with coordinates x:"+tempFeature.xStart+" y:"+tempFeature.yStart);
    			}
    			else if (tempSplit[0].contains("ELEVATOR")) {
    				while (ST.hasMoreTokens()) {
    					tempFeature.xStart = Integer.parseInt(ST.nextToken());
    					tempFeature.xEnd = tempFeature.xStart;
    					tempFeature.yStart = Integer.parseInt(ST.nextToken());
    					tempFeature.yEnd = tempFeature.yStart;
    				}
    				tempFeature.featureType = "ELEVATOR";
    				featureList.addFeature("ELEVATOR", tempFeature);
    				Log.i("parseMapFeatures", "Added feature ELV with coordinates x:"+tempFeature.xStart+" y:"+tempFeature.yStart);
    			}
    			else if (tempSplit[0].contains("STAIRS")) {
    				while (ST.hasMoreTokens()) {
    					tempFeature.xStart = Integer.parseInt(ST.nextToken());
    					tempFeature.xEnd = tempFeature.xStart;
    					tempFeature.yStart = Integer.parseInt(ST.nextToken());
    					tempFeature.yEnd = tempFeature.yStart;
    				}
    				tempFeature.featureType = "STAIRS";
    				featureList.addFeature("STAIRS", tempFeature);
    				Log.i("parseMapFeatures", "Added feature STAIRS with coordinates x:"+tempFeature.xStart+" y:"+tempFeature.yStart);
    			}
    			else {
    	            Log.i("parseMapfeatures", "Cannot parse line: " + featureStr);
    			}
    		}
    	}
    	else {
    		Toast.makeText(getApplicationContext(), "InputStream is null!", 1).show();
    	}
    	MapIS.close();
    }

}